﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DBTool
{
    using DB_TABLE_NAME = System.String;

    public partial class TriggerSpawnTableMeta : TableMeta
    {
        public TriggerSpawnTableMeta()
        {
            _createTableName.Add("trigger_spawn");
            _createTableName.Add("trigger_spawn_monster");

            _rowGropList.Add( new KeyValuePair<DB_TABLE_NAME, string[]>( "trigger_spawn", trigger_spawn ) );
            _rowGropList.Add( new KeyValuePair<DB_TABLE_NAME, string[]>( "trigger_spawn_monster", trigger_spawn_monster ) );
        }

        protected override void ExtraProcessByTable(string exportPath)
        {
            TriggerSpawnMonsterIdxSplit(exportPath);
        }

        private void TriggerSpawnMonsterIdxSplit(string exportPath)
        {
            string triggerSpawnMonsterIdxTablePath = exportPath + "/trigger_spawn_monster" + Config._exportFileExt;

            CSVReader triggerMonsterIdxTable = new CSVReader(triggerSpawnMonsterIdxTablePath);

            File.Delete(triggerSpawnMonsterIdxTablePath);

            // Sync Delete File
            while (true == File.Exists(triggerSpawnMonsterIdxTablePath)) { }

            StreamWriter fileWriter = new StreamWriter(triggerSpawnMonsterIdxTablePath, false, Encoding.UTF8);

            IList<string> rows = triggerMonsterIdxTable.GetRows();
            string[] titles = triggerMonsterIdxTable.GetColumnTitle();

            // write title
            fileWriter.WriteLine( rows[0] );

            for (int rowCount = 1; rowCount < triggerMonsterIdxTable.GetRowCount(); ++rowCount)
            {
                string triggerSpawnIdx = triggerMonsterIdxTable.Find( rowCount, "spawn_idx" );
                string[] splitedMonsterIdx = triggerMonsterIdxTable.Find( rowCount, "monster_idx" ).Split( ';' );
           
                foreach( string monIdx in splitedMonsterIdx )
                {
                    fileWriter.WriteLine( triggerSpawnIdx + "," + monIdx );
                }
            }
            
            fileWriter.Close();
        }
    }
}
